<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.ruleName" placeholder="规则名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker
        v-model="times"
        class="filter-item"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width: 250px;"
        type="daterange"
        value-format="yyyy-MM-dd"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规则名称" class-name="status-col" min-width="120">
        <template slot-scope="{row}">
          {{ row.ruleName }}
        </template>
      </el-table-column>
      <el-table-column label="渠道" min-width="120" align="center">
        <template slot-scope="{row}">
          <span v-if="row.channelId===0">全部渠道</span>
          <span v-else>{{ row.channelName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="触发报警时段" min-width="300" align="center">
        <template slot-scope="{row}">
          <span>{{ row.warningTimePeriods }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警时间" min-width="200" align="center">
        <template slot-scope="{row}">
          <span>{{ row.warningTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警文案" min-width="240" align="center">
        <template slot-scope="{row}">
          <span v-if="row.ruleId ===1">10分钟无新增注册数</span>
          <span v-if="row.ruleId ===2">注册数环比昨天同一时段下降10%</span>
          <span v-if="row.ruleId ===3">登录率同比上一小时下降10%</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="90" align="center">
        <template slot-scope="{row}">
          <span>{{ row.warnStatus ===0? '未确认':'已确认' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.warnStatus ===0" type="primary" size="mini" @click="handleUpdate(row)">
            确认
          </el-button>
          <el-button v-if="row.warnStatus ===1" type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="recordList"
    />


  </div>
</template>

<script>
import { getChannelIdAndNick } from '@/api/user'
import { recordList, ACKRecord, deleteRecord } from '@/api/sys'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Sysdict',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableData: [],
      total: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 10
      },
      sysDict: {},
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        username: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      optionsCopy: [],
      options: [],
      listLoading: false
    }
  },
  created() {
    this.recordList(this.listQuery)
  },
  methods: {
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    async getChannelIdAndNick() {
      const res = await getChannelIdAndNick(this.listQuery)
      this.optionsCopy[0] = { channel_name: '全部', channelId: 0 }
      this.options[0] = { channel_name: '全部', channelId: 0 }
      res.data.data.forEach(element => {
        if (element.channel_name !== null) {
          this.optionsCopy.push(element)
          this.options.push(element)
        }
      })
    },
    // 获取用户列表
    async recordList() {
      const res = await recordList(this.listQuery, this.listQuery.sysLabel)
      try {
        res.data.data.records.forEach(ele => {
          if (ele.ruleStatus === 0) {
            ele.ruleStatus = false
          } else if (ele.ruleStatus === 1) {
            ele.ruleStatus = true
          }
          if (ele.warningStatus === 0) {
            ele.warningStatus = false
          } else if (ele.warningStatus === 1) {
            ele.warningStatus = true
          }
        })
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleFilter() {
      this.listQuery.current = 1
      if (this.times == null) {
        this.listQuery.warningEndTime = ''
        this.listQuery.warningStartTime = ''
      } else if (this.times.length === 0) {
        this.listQuery.warningEndTime = ''
        this.listQuery.warningStartTime = ''
      } else {
        this.listQuery.warningEndTime = this.times[1]
        this.listQuery.warningStartTime = this.times[0]
      }
      this.recordList()
    },
    cancel() {
      this.recordList()
    },
    resetTemp() {
      this.sysDict = {}
    },
    handleCreate() {
      this.getChannelIdAndNick()
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    async handleUpdate(row) {
      ACKRecord({ id: row.id }).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: '确认成功',
            type: 'success',
            duration: 2000
          })
          this.cancel()
        } else {
          this.$notify({
            title: 'fail',
            message: '确认失败',
            type: 'fail',
            duration: 2000
          })
        }
      })
    },

    handleDelete(row) {
      deleteRecord({ id: row.id }).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.cancel()
        } else {
          this.$notify({
            title: 'fail',
            message: '删除失败',
            type: 'fail',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.pic{
  width: 150px;
  margin-top: 10px;
  margin-left: 10px;
}
.ok{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color:rgb(35, 248, 124)
}
.fail{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color: rgb(240, 174, 76);
}
.info{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
}
.info-name{
  margin-left: 50px;
  margin-top: 10px;
  color:rgb(172, 176, 177);
}
.title{
  margin-top: 10px;
  margin-left: 10px;
  font-size: 20px;
  font-weight: 400;
}
.box{
  margin-top: 10px;
  width: 100%;
  border: solid rgb(138, 219, 230) 2px;
}
.nick{
  font-size: 30px;
  font-weight: 600;
}
.link-top {
  margin-left: 10px;
  margin-top: 10px;
  width: 100%;
  height: 2px;
  border-top: solid rgb(138, 219, 230) 2px;
}
.link-title {
  margin-left: 10px;
  margin-top: 10px;
  width: 80px;
  height: 2px;
  border-top: solid rgb(172, 176, 177) 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
